<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/28 14:59
 */

namespace App\Project\Filters;

class FollowUpFilter extends FilterAbstract
{
    protected function forExtend()
    {
        $map = [
            'interval_evaluation.blood_glucose'        => 1,
            'interval_evaluation.weight.change'        => 1,
            'interval_evaluation.weight.effect'        => 1,
            'interval_evaluation.weight.kg'            => 1,
            'interval_evaluation.hypoglycemia'         => 1,
            'interval_evaluation.remark'               => 1,
            'interval_symptom.symptoms'                => 1,
            'interval_symptom.remark'                  => 1,
            'diet'                                     => 1,
            'diet.staple'                              => 1,
            'diet.meat'                                => 1,
            'diet.fat'                                 => 1,
            'diet.vegetable'                           => 1,
            'diet.fruit'                               => 1,
            'diet.salt'                                => 1,
            'diet.diet'                                => 1,
            'diet.breakfast'                           => 1,
            'diet.breakfastAdditional'                 => 1,
            'diet.lunch'                               => 1,
            'diet.lunchAdditional'                     => 1,
            'diet.dinner'                              => 1,
            'diet.dinnerAdditional'                    => 1,
            'sport'                                    => 1,
            'sport_time'                               => 1,
            'sport_strength'                           => 1,
            'normal.diet'                              => 1,
            'normal.sleep'                             => 1,
            'normal.spirit'                            => 1,
            'normal.emotion'                           => 1,
            'normal.shit.value'                        => 1,
            'normal.shit.frequency'                    => 1,
            'normal.shit.remark'                       => 1,
            'normal.shit.customFrequency'              => 1,
            'normal.DysuriaYN.frequency'               => 1,
            'normal.pee.frequency'                     => 1,
            'normal.pee.value'                         => 1,
            'normal.pee.remark'                        => 1,
            'normal.pee.difficulty'                    => 1,
            'normal.sweat.situation'                   => 1,
            'normal.sweat.remark'                      => 1,
            'thyroid'                                  => 1,    //  甲状腺情况
            'thyroid.started_at'                       => 1,    //  甲状腺情况 起病日期
            'thyroid.inducement'                       => 1,    //  甲状腺情况 诱因
            'thyroid.symptoms'                         => 1,    //  甲状腺情况 起病症状
            'thyroid.symptoms_remark'                  => 1,    //  甲状腺情况 起病症状 备注
            'thyroid.examination'                      => 1,    //  甲状腺情况 化验检查
            'thyroid.examination_remark'               => 1,    //  甲状腺情况 化验检查 备注
            'thyroid.hyperthyroidism'                  => 1,    //  甲状腺情况 甲亢相关症状
            'thyroid.hyperthyroidism_remark'           => 1,    //  甲状腺情况 甲亢相关症状 备注
            'adrenal_gland'                            => 1,    //  高血压肾上腺疾病
            'adrenal_gland.started_at'                 => 1,    //  高血压肾上腺疾病 起病日期
            'adrenal_gland.symptoms'                   => 1,    //  高血压肾上腺疾病 起病症状
            'adrenal_gland.symptoms_remark'            => 1,    //  高血压肾上腺疾病 起病症状 备注
            'adrenal_gland.accompanied_symptom'        => 1,    //  高血压肾上腺疾病 伴随症状
            'adrenal_gland.accompanied_symptom_remark' => 1,    //  高血压肾上腺疾病 伴随症状 备注
        ];

        $map['interval_symptom.symptoms']         = $this->symptoms($this->request->input('interval_symptom.symptoms'));
        $map['medication']                        = $this->medication($this->request->input('medication'));
        $map['cn_medication']                     = $this->cnMedication($this->request->input('cn_medication'));
        $map['thyroid.symptoms']                  = $this->symptoms($this->request->input('thyroid.symptoms'));
        $map['thyroid.examination']               = $this->symptoms($this->request->input('thyroid.examination'));
        $map['thyroid.hyperthyroidism']           = $this->symptoms($this->request->input('thyroid.hyperthyroidism'));
        $map['adrenal_gland.symptoms']            = $this->symptoms($this->request->input('adrenal_gland.symptoms'));
        $map['adrenal_gland.accompanied_symptom'] = $this->symptoms($this->request->input('adrenal_gland.accompanied_symptom'));

        return $map;
    }

    public function symptoms($symptoms)
    {
        if (is_array($symptoms)) {
            return array_pad([], count($symptoms), 1);
        }

        return 1;
    }

    protected function medication($medication)
    {
        $map = [];

        if (is_array($medication)) {
            foreach ($medication as $key => $item) {
                $map[$key] = array_flip([
                    'name',
                    'dosage.type',
                    'dosage.value',
                    'dosage.values.morning',
                    'dosage.unit',
                    'start_time',
                    'end_time',
                    'usage',
                    'frequency',
                    'remark',
                    'dosage.values.atnoon',
                    'dosage.values.evening',
                    'dosage.values.before_sleep'
                ]);
            }
        }

        return $map;
    }

    protected function cnMedication($cn_medication)
    {
        $map = [];

        if (is_array($cn_medication) && ($count = count($cn_medication)) > 0) {

            $keys = [];

            for ($i = 0; $i < $count; $i++) {

                $drugs = count($cn_medication[$i]['drugs']);

                for ($j = 0; $j < $drugs; $j++) {
                    $keys[$j] = array_flip(['name', 'weight']);
                }

                $map[$i] = array_merge(array_flip(['usage', 'dosage', 'frequency', 'dose']), ['drugs' => $keys]);
            }
        }

        return $map;
    }

    protected function getRootFieldsHasNoSon()
    {
        return [
            'record_id',
            'blood_glucose',
            'interval_evaluation',
            'interval_symptom',
            'medication',
            'cn_medication',
            'normal',
            'remark',
        ];
    }

    protected function getRootFieldsHasSon()
    {
        return ['blood_glucose'];
    }

    protected function getSonFieldsForEachRoot()
    {
        return ['limosis.low', 'limosis.high', 'postprandial.low', 'postprandial.high',];
    }
}